package me.zijunlin.sms.save.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import me.zijunlin.sms.save.model.Person;
import me.zijunlin.sms.save.model.Sms;
import android.database.Cursor;


public class DbConverter {
	
	private final String COL_NAME = "name";
	private final String COL_PHONE_NUMBER = "phone";

	public List<Sms> ConvertToSmsList(Cursor cursor) {
		List<Sms> smsList = new ArrayList<Sms>();

		while (cursor.moveToNext()) {
			Sms sms = new Sms();
			int col = cursor.getColumnIndex("_id");
			if (col != -1) {
				sms.setSmsId(cursor.getInt(col));
			}

			col = cursor.getColumnIndex("address");
			if (col != -1) {
				sms.setPhoneNumber(cursor.getString(col));
			}

			col = cursor.getColumnIndex("date");
			if (col != -1) {

				sms.setReceiveTime(new Date(cursor.getLong(col)));
			}

			col = cursor.getColumnIndex("body");
			if (col != -1) {
				sms.setSmsBody(cursor.getString(col));
			}

			col = cursor.getColumnIndex("subject");
			if (col != -1) {
				sms.setSmsSubject(cursor.getString(col));
			}

			col = cursor.getColumnIndex("person");
			if (col != -1) {
				sms.setContactId(cursor.getString(col));
			}

			smsList.add(sms);
		}
		cursor.close();
		return smsList;
	}

	public Person ConvertToPerson(Cursor cursor) {
		if(cursor==null)
			return null;
		Person person = new Person();
		if (cursor.moveToNext()) {
			int col = cursor.getColumnIndex(COL_NAME);
			if (col != -1) {
				person.setDisplayName(cursor.getString(col));
			}
		}
		cursor.close();
		return person;
	}

	
}
